#include "TextRenderFeature.h"
#include <SERender/Renderer/Renderer.h>
#include <SEngine/Render/Entity/Loader/TypeLoader.h>
#include <SECore/Manager.h>

namespace SEngine
{
    void TextRenderFeature::DrawTypes(char ch, glm::ivec2 size, glm::vec4 color, glm::mat4 modelMatrix)
    {
        AddChar(ch, size);
        auto & drawable = m_charTable.at(ToID(ch, size));
        drawable->GetMaterial().Value().Set("u_model", modelMatrix);
        drawable->GetMaterial().Value().Set("u_color", color);
        m_renderer.DrawEntity(*drawable);
    }
    
    TextRenderFeature::ID TextRenderFeature::ToID(char ch, glm::ivec2 size)
    {
        return std::to_string(size.x) + "_" + std::to_string(size.y) + "_" + ch;
    }
    
    void TextRenderFeature::AddChar(char ch, glm::ivec2 size)
    {
        if (m_charTable.find(ToID(ch, size)) == m_charTable.end()) {
            TTypesLoader loader;
            loader.SetFontTTF(Manager::GetInstance().Get<Font>("Font_RudaBold")->Value()).SetPixelSize(size.x, size.y).LoadChar(ch);
            auto drawable = std::make_unique<Drawable>();
            auto material = Manager::GetInstance().Get<Material>("Material_Text").value();
            // ResValue(material).Set("u_textMap", Shader::Sampler{loader.CreateTypeTexture(), TextureSlot::TEXTURE0});
            material.Value().GetPSO().blend = true;
            drawable->SetMesh(loader.CreateTypeMesh());
            drawable->SetMaterial(material);
            m_charTable.emplace(ToID(ch, size), std::move(drawable));
        }
    }
}